package routers

import (
	"gameserver/gcommon"
	"gameserver/messages"
	"gameserver/server/Login/internal"
	"gameserver/server/Login/internal/slistmgr"
	"gameserver/server/redisutil"
	"net/http"
	"time"

	"gitee.com/night-tc/gobige/common"
	"gitee.com/night-tc/gobige/logger"
	"github.com/gin-gonic/gin"
)

/*
登录后的接口请求
*/

// 玩家的服务器列表
func GetGroupList_Handler(c *gin.Context) {
	var err error
	defer func() {
		if err != nil {
			logger.Warnf("GetGroupList_Handler fail err:%v", err)
			if v, ok := err.(*common.Error); ok {
				c.JSON(http.StatusOK, gin.H{"Result": v.ErrCode})
			} else {
				c.JSON(http.StatusOK, gin.H{"Result": gcommon.RETMSG_Tips_Param_Fail})
			}
		}
	}()

	uid := c.GetUint64(internal.Gin_Get_UserID)
	sli := slistmgr.GetSliMgr().GetAreaList()
	pli, perr := getPlayerByUserID(uid)
	if perr != nil {
		err = perr
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Result":     gcommon.RETMSG_success,
		"ServerList": sli,
		"PlayerList": pli,
	})
}

// 创建角色
func CreatePlayer_Hander(c *gin.Context) {
	var err error
	defer func() {
		if err != nil {
			logger.Warnf("GetGroupList_Handler fail err:%v", err)
			if v, ok := err.(*common.Error); ok {
				c.JSON(http.StatusOK, gin.H{"Result": v.ErrCode})
			} else {
				c.JSON(http.StatusOK, gin.H{"Result": gcommon.RETMSG_Tips_Param_Fail})
			}
		}
	}()
	uid := c.GetUint64(internal.Gin_Get_UserID)
	msg := new(messages.CreatePlayerReq)
	err = c.ShouldBindJSON(msg)
	if err != nil {
		return
	}
	pmd, perr := CreatePlayerByUserID(uid, msg.GroupID, msg.PlayerName)
	if perr != nil {
		err = perr
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Result": gcommon.RETMSG_success,
		"Data":   pmd,
	})
}

// 开始游戏 获取对应角色的服务器链接信息
func GameStart_Handler(c *gin.Context) {
	var err error
	defer func() {
		if err != nil {
			logger.Warnf("GetGroupList_Handler fail err:%v", err)
			if v, ok := err.(*common.Error); ok {
				c.JSON(http.StatusOK, gin.H{"Result": v.ErrCode})
			} else {
				c.JSON(http.StatusOK, gin.H{"Result": gcommon.RETMSG_Tips_Param_Fail})
			}
		}
	}()
	uid := c.GetUint64(internal.Gin_Get_UserID)
	msg := new(messages.GameStartByPlayerReq)
	err = c.ShouldBindJSON(msg)
	if err != nil {
		return
	}
	umd, uerr := getUser(uid)
	if uerr != nil {
		err = uerr
		return
	}
	pmd, perr := getPlayer(msg.PlayerID)
	if perr != nil {
		err = perr
		return
	}
	if pmd.UserID != uid {
		err = gcommon.Err_User_Role_Not_Exist
		return
	}
	gmd := slistmgr.GetSliMgr().GetGroup(pmd.GroupID)
	if gmd == nil {
		err = gcommon.Err_Srv_Not_Exist
		return
	}
	now := time.Now()
	if gmd.SrvOpenTime > now.Unix() ||
		gmd.SrvStatus != messages.SrvStatusEnum_Normal {
		// 是否为白名单用户
		if !slistmgr.GetSliMgr().IsWhiteUser(umd.UserID, umd.OpenID, umd.ChannelID, umd.DeviceInfo) {
			err = gcommon.Err_Srv_No_Available //服务器维护中
			return
		}
	}

	// 角色信息缓存
	plrd := redisutil.NewLoginPlayer(pmd.PlayerID)
	_ = plrd.SaveInfo(map[string]any{
		redisutil.LoginPlayer_Key_PlayerID:   pmd.PlayerID,
		redisutil.LoginPlayer_Key_UserID:     pmd.UserID,
		redisutil.LoginPlayer_Key_GroupID:    pmd.GroupID,
		redisutil.LoginPlayer_Key_PlayerName: pmd.PlayerName,
	})

	outaddr, token, rerr := SetRedisPlayerGateway(pmd, umd, gmd.Group_MD)
	if rerr != nil {
		err = rerr
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Result": gcommon.RETMSG_success,
		"Data": map[string]any{
			"ServerAddr": outaddr,
			"Token":      token,
			"PlayerID":   pmd.PlayerID,
		},
	})

}
